/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Contactos.java
 *
 * Created on 19/11/2011, 12:14:02 AM
 */
package Interface;

import Dominio.Controladora;
import Dominio.Usuario;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Agustin
 */
public class Contactos extends javax.swing.JFrame {

    private static Contactos instance;

    public static Contactos getInstance() {
        if (instance == null) {
            instance = new Contactos();
        }
        instance.actualizarTablas();
        return instance;
    }

    /** Creates new form Contactos */
    public Contactos() {
        initComponents();
        this.setLocationRelativeTo(null);
        setEventoMouseClicked();
        actualizarTablas();

    }

    public void actualizarTablas() {
        ModeloTablaUsuarios contactos = new ModeloTablaUsuarios(0, "", 0);
        ModeloTablaSujerencias sujerencias = new ModeloTablaSujerencias();
        JTable tablaContactos = new JTable(contactos.getUsuarios(), contactos.getCabeceras());
        JTable tablaSujerencias = new JTable(sujerencias.getContactos(), sujerencias.getCabeceras());
        tblContactos.setModel(tablaContactos.getModel());
        tblSujerencias.setModel(tablaSujerencias.getModel());
    }

    private void setEventoMouseClicked() {

        tblSujerencias.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                tblSujerenciasMouseClicked(e);
            }
        });
    }

    private void tblSujerenciasMouseClicked(java.awt.event.MouseEvent evt) {
        int row = this.tblSujerencias.rowAtPoint(evt.getPoint());
        if (row >= 0 && tblSujerencias.isEnabled()) {
            if (tblSujerencias.getModel().getValueAt(row, 0) != null) {
                txtNombreContacto.setText(tblSujerencias.getModel().getValueAt(row, 0).toString());
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblContactos = new javax.swing.JLabel();
        txtNombreContacto = new javax.swing.JTextField();
        lblNombredeContacto = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSujerencias = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblContactos = new javax.swing.JTable();
        btnAgregarContacto = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblContactos.setFont(new java.awt.Font("Monospaced", 0, 24));
        lblContactos.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblContactos.setText("Contactos");

        lblNombredeContacto.setText("Nombre del Contacto");

        tblSujerencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblSujerencias);

        jTabbedPane1.addTab("Sugerencias", jScrollPane1);

        tblContactos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblContactos.setEnabled(false);
        jScrollPane2.setViewportView(tblContactos);

        jTabbedPane1.addTab("Contactos", jScrollPane2);

        btnAgregarContacto.setText("+");
        btnAgregarContacto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarContactoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNombreContacto, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 296, Short.MAX_VALUE)
                        .addComponent(btnAgregarContacto)
                        .addGap(38, 38, 38))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblNombredeContacto)
                        .addContainerGap(462, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(lblContactos, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblContactos)
                .addGap(18, 18, 18)
                .addComponent(lblNombredeContacto)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombreContacto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregarContacto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarContactoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarContactoActionPerformed
        try {
            Controladora.getInstance().crearContacto(Controladora.getInstance().getUsuario().getNombre_usuario(), txtNombreContacto.getText());
            actualizarTablas();
            txtNombreContacto.setText("");
            JOptionPane.showMessageDialog(this, "Se ha agregado al contacto", "Listo", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Controladora.getInstance().limpiarE(e), "Atencion", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnAgregarContactoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Contactos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarContacto;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblContactos;
    private javax.swing.JLabel lblNombredeContacto;
    private javax.swing.JTable tblContactos;
    private javax.swing.JTable tblSujerencias;
    private javax.swing.JTextField txtNombreContacto;
    // End of variables declaration//GEN-END:variables
}
